package com.lg.erp.controller.chenximou;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.chenximou.Order;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.chenximou.IOrderService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class OrderController {
    
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;
    
    //订单分页
    @RequestMapping("/order/{current}/{pageSize}/{key}")
    public JsonResult getOrderByPage(@PathVariable Integer current,@PathVariable Integer pageSize,@PathVariable String key ){
        System.out.println(pageSize);
        System.out.println(key);
        if (key.equals("0")){
            key="";
        }
        Page<Order> page=new Page<>(current,pageSize);
        IPage<Order> pages = orderService.selectOrderByPage(page,key);
        return new JsonResult(1,"ok",pages);
    }

    //所有订单
    @RequestMapping("/order")
    public JsonResult getAllOrder(){
        List<Order> orders = orderService.selectAllOrder();
        return new JsonResult(1,"ok",orders);
    }
    
    //新增订单
    @ResponseBody
    @PostMapping("/order")
    public boolean saveOrder(@RequestBody Order order){
        order.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        order.setDealTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            User user=(User) principal;
            SUser one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
            order.setSupplierId(one.getCompanyId()); 
        }
        System.out.println("公司id："+order.getSupplierId());
//        System.out.println("当前随机id："+order.getId());
//        System.out.println("当前时间："+order.getDealTime());
        boolean save = orderService.saveOrder(order);
        return save;
    }  
    
    //修改订单
    @ResponseBody
    @PutMapping("/order")
    public boolean updateOrder(@RequestBody Order order){
        order.setCustomId("1");
        order.setProductId("1");
        boolean update = orderService.updateOrder(order);
        return update;    
    }
    
    //删除订单
    @DeleteMapping("/order/{id}")
    public boolean removeOrder(@PathVariable String id){
        boolean remove = orderService.removeOrderById(id);
        return remove;
    }
    
    //条件模糊查询
    @ResponseBody
    @PostMapping("/order/select")
    public JsonResult selectOrderByConditions(@RequestBody Order order){

        System.out.println("orderId:"+order.getOrderId());
        System.out.println("dataValue:"+order.getDateValue());
        if(order.getDateValue().equals("NaN-NaN-NaN")||order.getDateValue().equals("1970-1-1")){
            order.setDateValue("");
        }
        List<Order> orders = orderService.selectOrderByConditions(order.getOrderId(), order.getDateValue());
        return new JsonResult(1,"ok",orders);
        
    }
    
    //根据状态查询订单
    @ResponseBody
    @PostMapping("/order/status")
    public JsonResult selectOrderByStatus(@RequestBody Order order){
        System.out.println("订单状态编号:"+order.getOrderStatus());
        List<Order> orders = orderService.selectOrderByStatus(order);
        System.out.println("状态查询订单集合:"+orders);
        return new JsonResult(1,"ok",orders);
    }

    //根据状态查询订单
    @ResponseBody
    @PostMapping("/order/status2")
    public JsonResult selectOrderByStatus2(@RequestBody Order order){
        System.out.println("订单状态编号:"+order.getAllstatus());
        List<Order> orders = orderService.selectOrderByStatus2(order);
        System.out.println("状态查询订单集合:"+orders);
        return new JsonResult(1,"ok",orders);
    }

    //排单订单分页
    @ResponseBody
    @PostMapping("/order/status/{current}/{pageSize}/{key}")
    public JsonResult getOrderByStatusPage(@PathVariable Integer current,@PathVariable Integer pageSize,@RequestBody Order order,@PathVariable String key){
        if (key.equals("0")){
            key="";
        }
        Page<Order> page=new Page<>(current,pageSize);
        IPage<Order> pages = orderService.selectOrderByStatusPage(page,order,key);
        return new JsonResult(1,"ok",pages);
    }

    //修改订单状态
    @ResponseBody
    @PostMapping("/order/changestatue")
    public JsonResult changeOrderStatusById(@RequestBody Order order){
        System.out.println("修改订单状态编号:"+order.getId());
        boolean b = orderService.updateOrderStatusById(order);
        System.out.println("修改订单状态:"+b);
        return new JsonResult(1,"ok",b);
    }

}
